#include "string.h"
#include "math.h"
#include "crc8.h"
#include "delay.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"
#include "queue.h"

#include "myTask.h"

#include "bsp_timer.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "bsp_adc.h"

#include "motorstepper.h"

#include "switch.h"
#include "Air_control.h"
#include "RS7_ABSencoder.h"
#include "RGB_Belt.h"
#include "armmotor.h"
#include "stepper.h"
#include "stepperctrl.h"
#include "RelayMotor.h"

#include "armaction.h"
#include "armeventctrl.h"
#include "usarttransmitter.h"
#include "cantransmitter.h"
#include "communication.h"
#include "servercomm.h"
#include "manage.h"
#include "DroneCtrl.h"
#include "MasterAppReset.h"





#define SENDDATATIME    4


extern QueueHandle_t Usart1Debug_Queue;
extern QueueHandle_t ServerCmd_Queue;	//串口2接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can_Tx_Queue;		//CAN信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Usart3_Rx_Queue;	//串口3接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Usart6_Rx_Queue;	//串口6信息队列句柄，用于中断和任务通讯

uint16_t Size_value[9] = {0};


extern TaskHandle_t StartTask_Handler;
extern TaskHandle_t Step_Motor_Task_Handler;
extern TaskHandle_t LightLimit_Task_Handler;
extern TaskHandle_t ArmEvent_Task_Handler;
extern TaskHandle_t Comunication_Task_Handler;
extern TaskHandle_t Sensor_Task_Handler;
extern TaskHandle_t Manage_Task_Handler;

//LightLimit_task
void LightLimit_task(void *pvParameters)
{
	while(1)
	{
		ArmMotorLimitCtrl();     //光电检测
		if(!Get_ArmReset())
			reset2isp();
		
		
		hard_watchdog_feed();//硬件喂狗		
        vTaskDelay(10);
	}
}

//step_motor_task
void step_motor_task(void *pvParameters)
{
	while(1)
	{
	    ArmMotorCtrl();
        vTaskDelay(10);
	}
}

//Armevent_task
void Armevent_task(void *pvParameters)
{
	while(1)
	{
		ArmEventHandle();
		green_led_blink();
		vTaskDelay(10);
	}
}



T_RSKTCN01_DATA  s_rsktcn01_data;
T_WEATHER_DATA s_weather_data;

#define HANGAR_MODE 2

uint8_t Send_flag = 0;
void comunication_task(void *pvParameters)
{
	static uint8_t send_cnt = 0;
	static uint8_t usart1queuebuf[USART1_RX_SIZE]      = {0};	
	static uint8_t serverqueuebuf[USART2_MAX_RECV_LEN] = {0};
	static uint8_t usart3queuebuf[USART3_MAX_RECV_LEN] = {0};
	static uint8_t usart6queuebuf[USART6_MAX_RECV_LEN] = {0};
	static uint8_t server_uploda_cnt = 5;
	Weather_Fun.Weather_Rain_Set();
    vTaskDelay(100);
    Weather_Fun.Weather_Rain_Set();
	while(1)
	{
		if(xQueueReceive(Usart1Debug_Queue,usart1queuebuf,10)==pdTRUE){
//			Drones_Data_Pase(usart1queuebuf);
			#if (HANGAR_MODE == 1)
				UsartProcess(usart1queuebuf,USART1_RX_SIZE);//解析处理串口1调试数据
				IapJump2Bootloader(serverqueuebuf);
			#elif (HANGAR_MODE == 2)
				ServerCmdPrase(usart1queuebuf,USART1_RX_SIZE);//解析服务器数据
			#endif
		}
		
		if(xQueueReceive(ServerCmd_Queue,serverqueuebuf,10)==pdTRUE){
			SeriverToID(serverqueuebuf);
			Server_Param_Pase(serverqueuebuf);
			Cjson_Prase((const char*)serverqueuebuf);
			ServerCmdPrase(serverqueuebuf,USART2_MAX_RECV_LEN);//解析服务器数据
			IapJump2Bootloader(serverqueuebuf);
		}
		
		if(xQueueReceive(Usart3_Rx_Queue,usart3queuebuf,10)==pdTRUE){
			Weather_Fun.Weather_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析温湿度计数据
			Rsktcn01_fun.RSKTCN01_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析气象站数据
			s_rsktcn01_data = Rsktcn01_fun.RSKTCN01_Get_Data();
			s_weather_data = Weather_Fun.Weather_Data_Get();
		}
		
		if(xQueueReceive(Usart6_Rx_Queue,usart6queuebuf,10)==pdTRUE){
			Rs7abs_fun.RS7ABS_Prase(usart6queuebuf,USART6_MAX_RECV_LEN);//解析绝对式编码器数据
			Rs7abs_fun.RS7ABS_Get_Abs();
		}
	
		if(Wait_Singal(&server_uploda_cnt,5))
		{
			send_cnt++;
			if(send_cnt==1){
				Sensor_Data_Packed1_Upload_JSON(s_rsktcn01_data,s_weather_data);//send to server
			}
			else if(send_cnt==2){
				Sensor_Data_Packed2_Upload_JSON();//send to server
			}
            else if(send_cnt==3){
                send_cnt = 0;
                Sensor_Data_Packed_Upload_Pc(s_rsktcn01_data,s_weather_data);//send to pc
            }
		}
		vTaskDelay(100);
	}
}

void sensor_task(void *pvParameters)
{
	static uint8_t rs_flag = 1;
	static uint8_t Send_Data_State = 0;
	static uint8_t Send_cnt = SENDDATATIME,cnt = 0;
	RELAY7_ON();//OPEN FAN
	while(1)
	{	
		Relay_Ctrl();//继电器
		
		switch(Send_Data_State)
		{
			case 0:
			{
////				Air_control_Fun.Air_control_Data_Ask();//制冷空调
				if(Wait_Singal(&Send_cnt,SENDDATATIME)) Send_Data_State++;
				
				break;
			}
			case 1:
			{
				Weather_Fun.Weather_Data_Ask();//气象站
				if(Wait_Singal(&Send_cnt,SENDDATATIME)) Send_Data_State++;
				break;
			}
			case 2:
			{
				Rsktcn01_fun.RSKTCN01_Data_Ask();//温湿度计
				if(Wait_Singal(&Send_cnt,SENDDATATIME)) Send_Data_State = 0;
				break;
			}
		}
		rs_flag ? (Rs7abs_fun.RS7ABS_Data_Ask(1),rs_flag = 0):(Rs7abs_fun.RS7ABS_Data_Ask(2),rs_flag = 1);
		if(Wait_Singal(&cnt,5)){
			SwitchTest();
		}
		Usart2_Send();
		red_led_blink();
		vTaskDelay(100);
	}
}



uint8_t flag = 0;

void manage_task(void *pvParameters)
{
	static uint8_t offline_time = 0;
	RELAY2_ON();
//	RELAY1_ON();
	while(1)
	{
		RGB_Manage();	
		
		if(Wait_Singal(&offline_time,100)) {OffLine_Manage();};
		
		Button_Control_Door();
		Button_Control_Clip();
		Button_Control_Charge();
		Heatingfan_Manage();
		Hangar_State_Manage();
		Flash_Manage();
        Other_Manage();
		vTaskDelay(50);	
		if(flag)
		{
			ArmMotorParamSet(STEPPER_ONE,STEPPER_ON,STEPPER_PRECISE_MODE,DOOR_OPEN_DIR,20,90000);
			ArmMotorParamSet(STEPPER_TWO,STEPPER_ON,STEPPER_PRECISE_MODE,DOOR_OPEN_DIR,20,90000);
			flag = 0;
		}
//		  Size_value[5] = uxTaskGetStackHighWaterMark(Manage_Task_Handler);
		Size_value[6] = xPortGetFreeHeapSize();
	}
}



